// pages/championProject/championProject.js
let http = require('../../utils/request.js')
let path=getApp().globalData.path
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '冠军之路',
    active: 0,
    areaArray: [],
    projectArray: [],
    areaIndex: 0,
    projectIndex: 0,
    areaNum: 0,
    projectNum: 0,
    baseurl:path
  },
  // 地点选择
  areaChange(e) {
    let _this = this
    let areaChooseId = _this.data.areaArray[e.detail.value].id
    let projectChooseId = _this.data.projectChooseId
    this.setData({
      areaIndex: e.detail.value,
      areaNum: 1,
      areaChooseId: _this.data.areaArray[e.detail.value].id
    })
    // 判断是否有项目ID分别取请求地点
    if (projectChooseId) {
      http.getHttp('/coach_list', {
        place_id: areaChooseId,
        project_id: projectChooseId
      }, {}).then(res => {
        this.setData({
          coachTmp: res.data.data
        })
      })
    }
    http.getHttp('/coach_list', {
      place_id: areaChooseId,
    }, {}).then(res => {
      this.setData({
        coachTmp: res.data.data
      })
    })
  },
  // 项目选择
  projectChange(e) {
    let _this = this
    let projectChooseId = this.data.projectArray[e.detail.value].id
    this.setData({
      projectIndex: e.detail.value,
      projectNum: 1,
      projectChooseId: _this.data.projectArray[e.detail.value].id
    })
    let areaChooseId = _this.data.areaChooseId
    // 判断是否有地址ID分别去请求项目
    if (areaChooseId) {
      http.getHttp('/coach_list', {
        place_id: areaChooseId,
        project_id: projectChooseId
      }, {}).then(res => {
        this.setData({
          coachTmp: res.data.data
        })
      })
    } else {
      http.getHttp('/coach_list', {
        place_id: areaChooseId,
      }, {}).then(res => {
        this.setData({
          coachTmp: res.data.data
        })
      })
    }
  },
  // 跳转教练详情
  goCoachDetail(e){
    let coachid=e.currentTarget.dataset.coachid
    wx.navigateTo({
      url: `/pages/coachDetail/coachDetail?coachid=${coachid}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 请求地址列表
    http.getHttp('/places', {
      area_id: wx.getStorageSync('areaid')
    }, {}).then(res => {
      this.setData({
        areaArray: res.data.data
      })
    })
    // 请求项目列表
    http.getHttp('/projects', {
      is_pay: 2,
      type: 2
    }, {}).then(res => {
      this.setData({
        projectArray: res.data.data
      })
    })
    //  请求教练列表
    http.getHttp('/coach_list', {
    }, {}).then(res => {
      console.log('初始教练列表',res)
      this.setData({
        coachTmp: res.data.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})